/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package mx.its.dsw2.escuela.view;

import java.util.List;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.event.ActionEvent;
import mx.its.dsw2.escuela.bl.CursoBlLocal;
import mx.its.dsw2.escuela.bl.MaestroBlLocal;
import mx.its.dsw2.escuela.bl.MateriaBlLocal;
import mx.its.dsw2.escuela.modelo.Curso;
import mx.its.dsw2.escuela.modelo.Maestro;
import mx.its.dsw2.escuela.modelo.Materia;

/**
 *
 * @author Maq-8
 */
@ManagedBean
@SessionScoped
public class GradeBean {

    @EJB
    private MateriaBlLocal materiaBl;
    @EJB
    private MaestroBlLocal maestroBl;
    @EJB
    private CursoBlLocal cursoBl;
    private List<Curso> lista;
    private Curso grade;
    private Maestro teacher;
    private Materia matter;
    private String accion;
    private int idMaestro;
    private int idMateria;

    public GradeBean() {
        grade = new Curso();
    }

    public List<Curso> getLista() {
        lista = cursoBl.getAll();
        return lista;
    }

    public Curso getGrade() {
        return grade;
    }

    public void setGrade(Curso grade) {
        this.grade = grade;
    }

    public Maestro getTeacher() {
        return teacher;
    }

    public void setTeacher(Maestro teacher) {
        this.teacher = teacher;
    }

    public Materia getMatter() {
        return matter;
    }

    public void setMatter(Materia matter) {
        this.matter = matter;
    }

    public int getIdMaestro() {
        return idMaestro;
    }

    public void setIdMaestro(int idMaestro) {
        this.idMaestro = idMaestro;
    }

    public int getIdMateria() {
        return idMateria;
    }

    public void setIdMateria(int idMateria) {
        this.idMateria = idMateria;
    }

    public String procesarAccion() {
        if (accion.equalsIgnoreCase("Nuevo")) {
            cursoBl.register(grade);
        } else {
            if (accion.equalsIgnoreCase("Editar")) {
                cursoBl.modify(grade);
            } else {
                if (accion.equalsIgnoreCase("Eliminar")) {
                    cursoBl.delete(grade);
                }
            }
        }
        accion = "";
        return "GradeList";
    }

    public String procesarLlamada() {
        String page = "";
        if (accion.equalsIgnoreCase("Editar") || accion.equalsIgnoreCase("Nuevo")) {
            page = "GradeCreateEdit";
        } else {
            if (accion.equalsIgnoreCase("Eliminar")) {
                page = "GradeList";
            }
        }
        return page;
    }

    public void prepararNuevo(ActionEvent evt) {
        accion = "Nuevo";
        grade = new Curso();
    }

    public void prepararEditar(ActionEvent evt) {
        accion = "Editar";
        int index = Integer.parseInt(evt.getComponent().getClientId().split(":")[2]);
        grade = lista.get(index);
    }

    public void prepararEliminar(ActionEvent evt) {
        accion = "Eliminar";
        int index = Integer.parseInt(evt.getComponent().getClientId().split(":")[2]);
        grade = lista.get(index);
    }
    
    public String procesarSeleccion() {
        grade.setIdMaestro(maestroBl.findById(idMaestro));
        grade.setIdMateria(materiaBl.findById(idMateria));
        return "GradeConfirm";
    }
}